﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class admin_ManageProduct : System.Web.UI.Page
{
    public static SqlConnection sqlConnection;
    public static DataTable objDataTable;
    public static String str = null;
    protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {
            ddlCategory.DataSource = Category_Session.Get_CategoryAsTable("Cate_ParentID = 0", "");
            ddlCategory.DataTextField = "Cate_Name";
            ddlCategory.DataValueField = "Cate_ID";
            ddlCategory.DataBind();
            BindDataGrid(1);

        }
        else
        {

        } 
    }
    #region Bind data to grid
    protected void BindDataGrid(int pageIndex)
    {
        try
        {
            grdProduct.DataSource = Product_Session.Get_ProductAsPaging(pageIndex, 15, "order by Pro_ID DESC", "", "", "");
            grdProduct.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    #endregion		
    

    protected void grdProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdProduct.PageIndex = e.NewPageIndex;
        BindDataGrid(grdProduct.PageIndex);
    }
    protected void grdProduct_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
         string strProductID = e.CommandArgument.ToString().Trim();
        string strCommand = e.CommandName;
        switch (strCommand)
        {

            case "delete_Product":
                try
                {
                    
                    Product product = Product_Session.Get_Product_ByID(int.Parse(strProductID));
                    if (product == null) return;
                    //Comments_session.Delete_Comments_Where("Comments.Pro_ID=" + strProductID);
                    Product_Session.Delete_Product(product.Pro_ID);

                    WebMsgBox.Show("Đã xóa thành công");
                    BindDataGrid(1);
                }
                catch (Exception ex)
                {
                    //throw ex;
                }
                break;        
        }
    }
    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        if (ddlCategory.SelectedValue == null) return;
        grdProduct.DataSource = Product_Session.Get_ProductAsPaging(1, 15, "", "", "where Cate_ID=" + ddlCategory.SelectedValue.ToString(), "");
        grdProduct.DataBind();
    }
    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnSearch_Click(null, null);
    }
}
